package com.woniuxy.hrp.service.Serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.hrp.entity.PageBean;
import com.woniuxy.hrp.entity.PayRecord;
import com.woniuxy.hrp.entity.Reservation;
import com.woniuxy.hrp.entity.Walletopr;
import com.woniuxy.hrp.mapper.PayRecordMapper;
import com.woniuxy.hrp.mapper.ReservationMapper;
import com.woniuxy.hrp.service.PayRecordService;
import com.woniuxy.hrp.service.WalletoprService;
import com.woniuxy.hrp.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: hrph
 * @ClassName PayRecordServiceImpl
 * @description:
 * @author: LRW
 * @create: 2022-09-01 02:04
 * @Version 2.0
 **/
@Service
public class PayRecordServiceImpl implements PayRecordService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private PayRecordMapper payRecordMapper;
    /***
     * @description: 查询指定患者的所有支付记录
     * @param: patientId
     * @return: java.util.List<com.woniuxy.hrp.entity.PayRecord>
     * @author Lrw
     * @date: 2022/9/5 0:32
     */
    @Override
    public List<PayRecord> getAllByPatientId(int patientId) {
        List<Reservation> reservations = reservationMapper.selectPayRecordsByPatientId(patientId);
        if (reservations.size() > 0) {
            List<PayRecord> payRecords = new ArrayList<>();
            for (int i = 0; i < reservations.size(); i++) {
                payRecords.add(reservations.get(i).getPayrecord());
            }
            return payRecords;
        } else {
            return null;
        }
    }

    /***
     * @description: 分页查询指定患者的支付记录
     * @param: patientId page
     * @return: com.woniuxy.hrp.entity.PageBean<com.woniuxy.hrp.entity.PayRecord>
     * @author Lrw
     * @date: 2022/9/5 0:32
     */
    @Override
    public PageBean<PayRecord> getAllByPatientIdAndPage(int patientId, int page) {
        List<Reservation> reservations = reservationMapper.selectPayRecordsByPatientId(patientId);
        if (reservations.size()>0){
            ArrayList<PayRecord> payRecords = new ArrayList<>();
            for (int i = 0; i < reservations.size(); i++) {
                payRecords.add(reservations.get(i).getPayrecord());
            }
            PageBean pageBean = new PageBean<>();
            int pageSize = 5;//每页显示条数
            Page<Object> p = PageHelper.startPage(page, pageSize);
            pageBean.setData(payRecords);
            pageBean.setCurrPage(page);
            pageBean.setCurrPageSize(pageSize);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalNums(payRecords.size());
            int totalPages = payRecords.size()/pageSize + (payRecords.size()%pageSize != 0 ? 1 :0);
            pageBean.setTotalPages(totalPages);
            return pageBean;
        }else {
            return null;
        }

    }

    @Override
    public void add(PayRecord payRecord) {
        payRecordMapper.insert(payRecord);
    }

    /***
     * @description: 多条件分页查询支付记录
     * @param: 患者id、分页大小、当前页码 、查询条件
     * @return: com.woniuxy.hrp.entity.PageBean<com.woniuxy.hrp.entity.PayRecord>
     * @author Lrw
     * @date: 2022/9/7 16:50
     */
    @Override
    public PageBean<PayRecord> getPayRecordsByPatientIdAndConditionAndPage(int patientId, int pageSize, int page, PayRecord payRecord) {
        List<PayRecord> payRecords = payRecordMapper.selectRecordsByCondition(patientId, payRecord);
        List data = QueryUtil.getByPage(payRecords, pageSize, page);
        PageBean<PayRecord> pageBean = new PageBean<>();
        pageBean.setData(data);
        pageBean.setTotalNums(payRecords.size());
        pageBean.setTotalPages((payRecords.size() + pageSize -1) / pageSize);
        return pageBean;
    }
}
